const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  entry: path.resolve(__dirname, 'src/index.js'),
  output: {
    filename: 'ng-components.js',
    path: path.resolve(__dirname, 'dist'),
    library: {
      name: 'ngComponents',
      type: 'umd'
    },
    globalObject: 'this',
    clean: true
  },
  externals: {
    angular: {
      commonjs: 'angular',
      commonjs2: 'angular',
      amd: 'angular',
      root: 'angular'
    }
  },
  target: ['web', 'es5'],
  module: {
    rules: [
      {
        test: /\.less$/i,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader']
      },
      {
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      },
      {
        test: /\.html$/i,
        type: 'asset/source'
      },
      {
        test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'assets/[hash][ext][query]'
        }
      }
    ]
  },
  devServer: {
    static: [
      {
        directory: path.join(__dirname, 'demo'),
        publicPath: '/',
        watch: true
      },
      {
        directory: path.join(__dirname, 'dist'),
        publicPath: '/dist'
      }
    ],
    devMiddleware: {
      writeToDisk: true
    },
    host: '127.0.0.1',
    port: 8080,
    hot: false,
    open: '/',
    allowedHosts: 'all',
    client: {
      overlay: true
    }
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'ng-components.css'
    })
  ],
  resolve: {
    extensions: ['.js']
  },
  devtool: 'source-map'
};
